package com.xyy.ec.pop.api.kingdinggmp.service.impl;

import com.alibaba.fastjson.JSON;
import com.xyy.ec.pop.api.kingdinggmp.dto.pop.TaskQueryParam;
import com.xyy.ec.pop.api.kingdinggmp.service.SecurityService;
import com.xyy.ec.pop.api.kingdinggmp.utils.IpUtils;
import com.xyy.ec.pop.api.kingdinggmp.utils.MD5Util;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.Map;

@Service
public class SecurityServiceImpl implements SecurityService {
    /**
     * 加密操作
     *
     * @param taskQueryParam
     * @return
     */
    @Override
    public String encryption(TaskQueryParam taskQueryParam) {
        Integer taskId = taskQueryParam.getTaskId();
        if (taskId != null && taskId != 19001) {
            taskQueryParam.setTaskId(taskId);
        }
        if (taskId == 19001) {
            String ipAddress = "";
            InetAddress address = IpUtils.getLocalHostLANAddress();
            if (address != null) {
                ipAddress = address.getHostAddress();
            }
            taskQueryParam.setVersion("3.6");
            taskQueryParam.setJsonData(ipAddress);
        }
        if (taskId == 10086 || taskId == 19001) {
            taskQueryParam.setTaskId(null);
        }
        Map<String, Object> popParamMap = JSON.parseObject(JSON.toJSONString(taskQueryParam)).getInnerMap();
        popParamMap.remove("appSecret");
        taskQueryParam.setSign(MD5Util.encrypt(popParamMap, taskQueryParam.getAppSecret()));
        Map<String, Object> popParamMapNew = JSON.parseObject(JSON.toJSONString(taskQueryParam)).getInnerMap();
        popParamMapNew.remove("appSecret");
        return JSON.toJSONString(popParamMapNew);
    }

}
